package Leetcode第一期;

/**
 * @author : K k
 * @date : 10:50 2020/8/27
 */
public class 删除链表的倒数第N个节点_19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode temp=new ListNode(0);
        temp.next=head;
        ListNode p=temp,q=temp;
        int count=0;
        while (p!=null){
            p=p.next;
            if (count>n){
                q=q.next;
            }
            count++;
        }
        q.next=q.next.next;
        return temp.next;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
